<?php
namespace CGJ\Controller;

use Base\ConstDir\Api\ApiError;
use CGJ\Controller;
use Base\ConstDir\Redis;

class Api extends Controller{
    /**
     * @var string 版本号
     */
    protected $_version;

    /**
     * @var string 控制器名称
     */
    protected $_controllerName;

    /**
     * @var string 动作名称
     */
    protected $_actionName;
    /**
     * @var array 用户信息
     */
    protected $_memberInfo;

    /**
     * @var int 页码
     */
    protected $_page = 1;

    /**
     * @var int 单页数据条数
     */
    protected $_limit = 10;

    public function preDispatch()
    {
        //var_dump(strtoupper(md5('sign:Token55b0b5a421f79PayType2ClientComment%E5%8F%91%E8%B4%A7%E5%95%A6%E5%95%A6%F0%9F%98%84versionName1.5systemandroidCarModel109021UseRechargeMoney1UnitePayID1507230609413605')));die;
        parent::preDispatch();
        $this->_controllerName = $this->route->getParam('__CONTROLLER__');
        $this->_actionName = $this->route->getParam('action');
        $this->_version = $this->route->getParam('version');
        $signature = strtolower($this->request->getPost('Signature'));
        if(empty($signature) || (getenv('APP_ENV') != 'test' && $signature != $this->genSignature())){
            return $this->_return(ApiError::SIGNATUE_IS_NOT_VALID, ApiError::SIGNATURE_IS_NOT_VALID_MSG);
        }

        $token = $this->request->getPost('Token');
        $this->_page = $this->request->getPost('Page');
        $this->_limit = $this->request->getPost('Limit');

        if (!empty($token) && $this->_controllerName != 'login') {
            $tokenInfo = $this->sm->get('Api\Model\Token')->select(array('Token' => $token))->current();

            if (empty($tokenInfo)) {
                return $this->_return(ApiError::TOKEN_IS_NOT_VALID, ApiError::TOKEN_IS_NOT_VALID_MSG);
            } else {
                $memberInfoModel = $this->sm->get('Api\Model\MemberInfo');
                $select = $memberInfoModel->getSql()->select();
                $select->join(array('b' => 'CityStation'), 'MemberInfo.CityStationID = b.CityStationID', array('CityStationName'));
                $select->where(array('MemberID' => $tokenInfo['MemberID']));
                $this->_memberInfo = $memberInfoModel->selectWith($select)->current();
                if(!empty($this->_memberInfo)){
                    $redisObj = $this->sm->get('Base\Cache\Redis');
                    $this->_memberInfo['HomeProvinceName'] = $redisObj->hGet(Redis::REGION, $this->_memberInfo['HomeProvinceID']);
                    $this->_memberInfo['HomeCityName'] = $redisObj->hGet(Redis::REGION, $this->_memberInfo['HomeCityID']);
                    $this->_memberInfo['HomeAreaName'] = $redisObj->hGet(Redis::REGION, $this->_memberInfo['HomeAreaID']);
                    $this->_memberInfo['HometownProvinceName'] = $redisObj->hGet(Redis::REGION, $this->_memberInfo['HometownProvinceID']);
                    $this->_memberInfo['HometownCityName'] = $redisObj->hGet(Redis::REGION, $this->_memberInfo['HometownCityID']);
                    $this->_memberInfo['HometownAreaName'] = $redisObj->hGet(Redis::REGION, $this->_memberInfo['HometownAreaID']);
                    $this->sm->get('Api\Model\Member')->update(array('LastInviteTime' => time()), array('MemberID' => $this->_memberInfo['MemberID']));
                }
            }
        }

    }
}